package com.csu.backmanagesys.Controller;

import com.csu.backmanagesys.Service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;

@RequestMapping("/Products")
@Controller
public class ProductController
{
    @Autowired
    public ProductService productService;

    @RequestMapping("")
    public ModelAndView products(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("product",productService.selectProduct());
        modelAndView.setViewName("product_manager");
        return modelAndView;
    }

    @GetMapping("/singleProduct")
    public ModelAndView selectProduct(ModelAndView modelAndView,String search){
        modelAndView.addObject("product",productService.selectProductById(search));
        modelAndView.setViewName("product_manager");
        return modelAndView;
    }
    @DeleteMapping("/singleProduct")
    public ModelAndView deleteProduct(ModelAndView modelAndView,String productId){
        productService.deleteProduct(productId);
        modelAndView.addObject("product",productService.selectProduct());
        modelAndView.setViewName("product_manager");
        return modelAndView;
    }


    @PostMapping("/singleProduct") //可以用name进行唯一标识
    public ModelAndView addNewProduct(HttpServletRequest httpServletRequest)
    {
        ModelAndView modelAndView = new ModelAndView();
        String productId = httpServletRequest.getParameter("productId");
        String categoryId = httpServletRequest.getParameter("categoryId");
        if(productId.length() != 0 && categoryId.length() != 0){
            productService.insertProduct(
                    httpServletRequest.getParameter("productId"),
                    httpServletRequest.getParameter("categoryId"),
                    httpServletRequest.getParameter("name"),
                    httpServletRequest.getParameter("description")
            );
        }
        modelAndView.addObject("product",productService.selectProduct());
        modelAndView.setViewName("product_manager");
        return modelAndView;
    }

    @PostMapping("/update_product_information")
    public ModelAndView updateProduct (HttpServletRequest req){

        String productId = req.getParameter("productId ");
        String categoryId = req.getParameter("categoryId");
        String name = req.getParameter("name");
        String descn = req.getParameter("description ");
        productService.updateProduct(productId,categoryId,name,descn);
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("product",productService.selectProduct());
        modelAndView.setViewName("product_manager");
        return modelAndView;
    }
}
